package com.easy.cms.material.controller;

import com.easy.cms.base.dto.ListPageQuery;
import com.easy.cms.base.dto.PaginationQuery;
import com.easy.cms.base.dto.SearchQuery;

import com.easy.cms.material.model.Organization;
import com.easy.cms.material.repository.OrganizationRepository;
import com.easy.cms.base.CommonResult;
import jakarta.annotation.Resource;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/organizations")
public class OrganizationController {

    @GetMapping("/listPage")
    public CommonResult<List<Organization>> getOrganizationsByPage(@ModelAttribute ListPageQuery query) {
        // 此处需要根据实际情况替换为存在的方法
        return CommonResult.success(organizationRepository.findAll());
    }
    @Resource
    private OrganizationRepository organizationRepository;

    @GetMapping
    public CommonResult<List<Organization>> getAllOrganizations() {
        return CommonResult.success(organizationRepository.findAll());
    }

    @GetMapping("/search")
    public CommonResult<List<Organization>> searchOrganizations(@ModelAttribute SearchQuery query) {
        return CommonResult.success(organizationRepository.findByNameContainingAndOrgType(query.getName(), query.getOrgType()));
    }

    @PostMapping
    public CommonResult<Organization> createOrganization(@RequestBody Organization organization) {
        return CommonResult.success(organizationRepository.save(organization));
    }

    @PutMapping("/{id}")
    public CommonResult<Organization> updateOrganization(@PathVariable Long id, @RequestBody Organization organizationDetails) {
        Organization organization = organizationRepository.findById(id)
               .orElseThrow(() -> new RuntimeException("Organization not found with id: " + id));
        organization.setName(organizationDetails.getName());
        organization.setOrgType(organizationDetails.getOrgType());
        return CommonResult.success(organizationRepository.save(organization));
    }
    @DeleteMapping("/{id}")
    public CommonResult<?> deleteOrganization(@PathVariable Long id) {
        organizationRepository.deleteById(id);
        return CommonResult.success(null);
    }
}